<?php
/*
name:ror_way
mail:areless@gmail.com
QQ:8228680
host: http://code.google.com/p/phpmysqlror/
*/
date_default_timezone_set('PRC');
define('my_user', 'username'); /*input*/
define('my_pass', 'password');
define('my_dbname', 'db'); 
define('my_host', 'localhost');
class db
{
		public $table;
		public $where;
		public $where_value;
		public $savetemp;
		public $LIMIT;
		public $ORDER;
		public $hack;
		public function db($user,$pass,$dbname,$host)
		{
		$link=@mysql_connect($host, $user, $pass);
		mysql_select_db($dbname, $link);
		mysql_query("set names 'utf8'");
		}
		public function find_all ( $value='user' )
		{
						if ($this->LIMIT) {
								$myLIMIT=' limit '.$this->LIMIT;
						}
						$query="select * from ".$value.$myLIMIT;
						$r=@mysql_query($query);
						if (!$r || empty($r)) {
								return false;
						}
						$num=0;
						while ( $row=mysql_fetch_array($r, MYSQL_ASSOC))
						{
						foreach ($row as $key => $value)
						{
										$backr[$key][$num]=$value;
						}
						$num++;
						}
						mysql_free_result($r);
						return  $backr;
		}
		public function find_by ($value=null)
		{
						if ($this->LIMIT) {
								$myLIMIT=' limit '.$this->LIMIT;
						}
						if ( $this->hack )
						{
						$query="select * from ".$this->table.' where '.$this->hack.($value?$value:$this->where).' in ('.$this->where_value.')'.(($this->ORDER)?(' ORDER BY '.$this->ORDER):' ORDER BY id DESC').$myLIMIT;
						}else
						{
						$query="select * from ".$this->table.' where '.($value?$value:$this->where).' in ('.$this->where_value.')'.(($this->ORDER)?(' ORDER BY '.$this->ORDER):' ORDER BY id DESC').$myLIMIT;
						}
						$r=@mysql_query($query);
						if (!$r || empty($r)) {
								return false;
						}
						$num=0;
						while ( $row=mysql_fetch_array($r, MYSQL_ASSOC))
						{
						foreach ($row as $key => $value)
						{
										$backr[$key][$num]=$value;
						}
						$num++;
						}
						mysql_free_result($r);
						return  $backr;
		}
				public function find_count ($value=null)
		{
						if ($this->LIMIT) {
								$myLIMIT=' limit '.$this->LIMIT;
						}
						if ( $this->hack )
						{
						$query="select count(*) from ".$this->table.' where '.$this->hack.($value?$value:$this->where).' in ('.$this->where_value.')'.(($this->ORDER)?(' ORDER BY '.$this->ORDER):' ORDER BY id DESC');
						}else
						{
						$query="select count(*) from ".$this->table.' where '.($value?$value:$this->where).' in ('.$this->where_value.')'.(($this->ORDER)?(' ORDER BY '.$this->ORDER):' ORDER BY id DESC');
						}
						$r=@mysql_query($query);
						if (!$r || empty($r)) {
								return false;
						}
						while ( $row=mysql_fetch_array($r, MYSQL_ASSOC))
						{
						$backr=$row['count(*)'];
						}
						mysql_free_result($r);
						return  $backr;
		}
		public function like_count($value=null){
						if ($this->LIMIT) {
								$myLIMIT=' limit '.$this->LIMIT;
						}
						if ( $this->hack )
						{
						$query="select count(*) from ".$this->table.' where '.$this->hack.($value?$value:$this->where).' like ('.$this->where_value.')'.(($this->ORDER)?(' ORDER BY '.$this->ORDER):' ORDER BY id DESC');
						}else
						{
						$query="select count(*)  from ".$this->table.' where '.($value?$value:$this->where).' like ('.$this->where_value.')'.(($this->ORDER)?(' ORDER BY '.$this->ORDER):' ORDER BY id DESC');
						}
						$r=@mysql_query($query);
						if (!$r || empty($r)) {
								return false;
						}
						while ( $row=mysql_fetch_array($r, MYSQL_ASSOC))
						{
						$backr=$row['count(*)'];
						}
						mysql_free_result($r);
						return  $backr;
		}
				public function like_by ($value=null)
		{
						if ($this->LIMIT) {
								$myLIMIT=' limit '.$this->LIMIT;
						}
						if ( $this->hack )
						{
						$query="select * from ".$this->table.' where '.$this->hack.($value?$value:$this->where).' like ('.$this->where_value.')'.(($this->ORDER)?(' ORDER BY '.$this->ORDER):' ORDER BY id DESC').$myLIMIT;
						}else
						{
						$query="select * from ".$this->table.' where '.($value?$value:$this->where).' like ('.$this->where_value.')'.(($this->ORDER)?(' ORDER BY '.$this->ORDER):' ORDER BY id DESC').$myLIMIT;
						}
						$r=@mysql_query($query);
						if (!$r || empty($r)) {
								return false;
						}
						$num=0;
						while ( $row=mysql_fetch_array($r, MYSQL_ASSOC))
						{
						foreach ($row as $key => $value)
						{
										$backr[$key][$num]=$value;
						}
						$num++;
						}
						mysql_free_result($r);
						return  $backr;
		}
		public function save ()
		{
						$id=1;
						$query='UPDATE '.$this->table.' SET ';
						foreach ($this->savetemp as $key => $value)
						{
						$value=eregi_replace("'","''",$value);
										if ( $id==1 )
										{
										$query.=$key.'=\''.$value.'\'';
										$savel=$key;
										$saver='\''.$value.'\'';
										}else{
										$query.=','.$key.'=\''.$value.'\'';
										$savel.=','.$key;
										$saver.=',\''.$value.'\'';
										}
										$id++;
						}
						$query.=' where '.$this->where.' in ('.$this->where_value.')';
						if ( $this->where )
						{
						@mysql_query($query);
						return  true;
						}else{
						$query='INSERT INTO '.$this->table.' ('.$savel.') VALUES ('.$saver.')';
						@mysql_query($query);
						return  true;
						}
		}
		public function del ($value=null,$val=null)
		{
				$query='delete from '.$this->table.' where '.($val?$val:$this->where).'='.($value?$value:$this->where_value);
				@mysql_query($query);
				return  true;
		}
}
$db=new db(my_user,my_pass,my_dbname,my_host);
/*
=========find all
$db->LIMIT='0,10';
$aa=$db->find_all('albums');
print_r($aa);
=========find one;
$db->table='albums';
$db->where_value='1';
$bb=$db->find_by('id');
echo '<br>';
print_r($bb);
========insert;
$db->savetemp=array('artist'=>'a','title '=>'b');
$db->save();
========update;
$db->where='id'; /if where input turn update/
$db->savetemp=array('artist'=>'a','title '=>'b');
$db->save();
*/
?>

